package hai.com.android_test.widget.calendarView;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.Calendar;

/**
 * Created by 黄海 on 2017/6/20.
 */

public class CalendarView extends ViewPager {
    private static final String TAG = "CalendarView";
    private Calendar currentDate = Calendar.getInstance();
    private OnMonthSelectListener onMonthSelectListener;
    MonthFragment months[] = new MonthFragment[4];
    public CalendarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        for (int i = 0; i < months.length; i++) {
            Calendar temp = (Calendar) currentDate.clone();
            temp.roll(Calendar.MONTH, -1 + i);
            months[i] = new MonthFragment(i);
            months[i].updateDate(temp);
        }
        setAdapter(new FragmentPagerAdapter(((FragmentActivity) context).getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return months[position % months.length];
            }

            @Override
            public int getCount() {
                return 400;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                return super.instantiateItem(container, position % months.length);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                super.destroyItem(container, position % months.length, object);
            }
        });
        addOnPageChangeListener(new SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                Calendar currentDate = months[position % months.length].getCurrentDate();
                if (onMonthSelectListener != null)
                    onMonthSelectListener.onMonthSelect(months[position % months.length].getCurrentDate());

                Calendar next2Date = (Calendar) currentDate.clone();
                next2Date.add(Calendar.MONTH, 1);//下个月
                months[(position+1)%months.length].updateDate(next2Date);
                Calendar lastDate = (Calendar) currentDate.clone();
                lastDate.add(Calendar.MONTH, -1);
                months[(position-1)%months.length].updateDate(lastDate);
            }
        });

        int i = getAdapter().getCount() / 2 % months.length;
        int offset = i > 1 ? i - 1 : 1 - i;
        setCurrentItem(getAdapter().getCount() / 2 + (i > 1 ? -offset : offset));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = 0;
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.measure(widthMeasureSpec,
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int h = child.getMeasuredHeight();
            if (h > height) //采用最大的view的高度。
                height = h;
        }
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
                MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public interface OnMonthSelectListener {
        void onMonthSelect(Calendar date);

    }

    public void setOnMonthSelectListener(OnMonthSelectListener onMonthSelectListener) {
        this.onMonthSelectListener = onMonthSelectListener;
    }
}
